package com.tencent.liteav.demo.play.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.tencent.liteav.basic.log.TXCLog;
import com.tencent.liteav.demo.play.SuperPlayerConst;
import com.tencent.liteav.demo.play.SuperPlayerModel;
import com.tencent.liteav.demo.play.activity.LockScreenActivity;
import com.tencent.liteav.demo.play.bean.BaseEvent;
import com.tencent.liteav.demo.play.bean.PlayInfoBean;
import com.tencent.rtmp.TXVodPlayer;

import org.greenrobot.eventbus.EventBus;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {

    private boolean isContinue = true;
    private MyBinder binder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
//        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
//        registerReceiver(mReceiver,filter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while ( isContinue ){
                    if( binder != null){
                        try {
                            Thread.sleep( 1000 );
                            binder.notifyRefreshUI();
                        } catch (InterruptedException e) {

                        }
                    }
                }
            }
        }).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand( intent , flags , startId );
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return binder;
    }

    public class MyBinder extends Binder{

        private TXVodPlayer txVodPlayer;
        private PlayInfoBean playInfoBean;
        private int currentPlayDurationTotal;
        //当前开始播放
        private int startTime;
        private Timer playTimer;
        //注意: isPlaying()或isResume都为true,才为正在播放
        private boolean isResume;

        public void setTXVodPlayer( TXVodPlayer txVodPlayer ){
            this.txVodPlayer = txVodPlayer;
        }

        public TXVodPlayer getTxVodPlayer() {
            return txVodPlayer;
        }

        public void setPlayInfoBean(PlayInfoBean playInfoBean) {
            this.playInfoBean = playInfoBean;
        }

        public PlayInfoBean getPlayInfoBean() {
            return playInfoBean;
        }

        public boolean isPlaying(){
            if( txVodPlayer != null ){
                return txVodPlayer.isPlaying();
            }
            return false;
        }

        public void resumePlay(){
            if( txVodPlayer != null ){
                txVodPlayer.resume();
            }
        }

        public void pausePlay(){
            if( txVodPlayer != null ){
                txVodPlayer.pause();
                pauseCountTime();
                isResume = false;
            }
        }

        public void release(){
            if (txVodPlayer != null) {
                txVodPlayer.setVodListener(null);
                txVodPlayer.stopPlay(false);
                txVodPlayer = null;
            }
        }

        public int getCurrentPlayDurationTotal() {
            return currentPlayDurationTotal;
        }

        public void setCurrentPlayDurationTotal(int currentPlayDurationTotal) {
            this.currentPlayDurationTotal = currentPlayDurationTotal;
        }

        public int getStartTime() {
            return startTime;
        }

        public void setStartTime(int startTime) {
            this.startTime = startTime;
        }

        /**
         * 开始计时
         */
        public void startTimer(){
            if( playTimer == null ){
                playTimer = new Timer();
                playTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if( isPlaying() || isResume ){
                            startTime += 1;
                        }
                    }
                } , 0 , 1000 );
            }
        }

        /**
         * 暂停计时
         */
        public void pauseCountTime() {
            //暂停先累加然后清0
            setCurrentPlayDurationTotal( getCurrentPlayDurationTotal() + getStartTime() );
            setStartTime( 0 );
        }

        /**
         * 取消计时
         */
        public void cancelTimer(){
            if( playTimer == null ){
                return;
            }
            playTimer.cancel();
            playTimer = null;
            startTime = 0;
            currentPlayDurationTotal = 0;
        }

        public boolean isResume() {
            return isResume;
        }

        public void setResume(boolean resume) {
            isResume = resume;
        }

        public void notifyRefreshUI(){
            EventBus.getDefault().post( new BaseEvent<String>(
                    BaseEvent.EventCodeContains.REFRESH_PLAY_INFO , BaseEvent.EventNameContains.REFRESH_PLAY_INFO , "" , null ));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        unregisterReceiver( mReceiver );
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                Intent lockscreen = new Intent(MyService.this, LockScreenActivity.class);
                lockscreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(lockscreen);
            }
        }
    };
}
